package cate.game.activity.mtowerchallenge;

import cate.common.table.activity.mtower.MTowerLevelRow;
import cate.common.table.d.GDFight;
import cate.common.table.d.GDOperation;
import cate.common.table.farm.fbchallenge.row.FbChallengeRow;
import cate.game.activity.mtowerchallenge.msg.MTowerChallengeResultResp;
import cate.game.activity.mtowerchallenge.po.MTowerLevel;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;
import cate.game.role.farm.FightChallengeHandler;

import java.util.ArrayList;
import java.util.List;

public class MTowerFTC extends AbstractFarmFTC<MTowerChallengeActivity> {
    /**
     * 当前挑战关卡的模板
     */
    private final MTowerLevelRow row;
    @Override
    protected MTowerChallengeActivity po(Role role) {
        return role.getActivity().getEnableActivitySingle(funcId);
    }

    public MTowerFTC(Role role, int funcId, MTowerLevelRow row) {
        this.funcId = funcId;
        this.row = row;
        setup(role);
    }

    @Override
    public void handleResult(Role role, PlayPO playPO) {
        super.handleResult(role, playPO);
        boolean win = playPO.winner == GDFight.Team.A;
        MTowerChallengeActivity mTower = po(role);
        MTowerChallengeResultResp resp = new MTowerChallengeResultResp();
        resp.win = win;
        if (win) {
            MTowerLevel level = mTower.showLevel(row.id);
            MixRes reward = new MixRes();
            //首通奖励
            role.log.setOperation(GDOperation.M_TOWER);
            if (!level.pass) {
                reward.addList(new MixRes(row.firstRewardStr));
            } else {
                //reward.addList(new MixRes(row.mopRewardStr));
            }
            reward.add(role, true, GDOperation.M_TOWER);
            role.log.clearOperation();
            resp.reward = reward;
            level.pass = true;
            // 本次战斗有没有完成新的挑战
            boolean challengeChange = false;
            if (level.challenge.size() < row.challengeIds.size()) {
                // 这里要检测战斗挑战的完成情况
                List<Integer> starDone = new ArrayList<>();
                //星级挑战
                for (int challengeId : row.challengeIds) {
                    FbChallengeRow challengeRow = role.getGame().table.fbChallenge.spiceFb.get(challengeId);
                    if (challengeRow == null) {
                        continue;
                    }
                    boolean done = FightChallengeHandler.checkDone(role.getGame(), playPO, challengeRow);
                    if (done) {
                        starDone.add(challengeId);
                    }
                }
                if (starDone.size() >= level.challenge.size()) {
                    // 只检测之前没有完成的星级挑战
                    for (int challengeId : starDone) {
                        if (!level.challenge.contains(challengeId)) {
                            challengeChange = true;
                            level.challenge.add(challengeId);
                        }
                    }
                }
            }
            //有挑战更新
            if (challengeChange) {
                mTower.updateLevel();
            }
            mTower.noticeUpdate();
        }
        role.sendNow(resp);
    }
}
